//
//  UIImageView+IconObserver.m
//  DeallocTest
//
//  Created by 冷悦 on 16/3/24.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "UIImageView+IconObserver.h"
#import "IconContext.h"
#import "UIImageView+WebCache.h"
#import <objc/runtime.h>
#import "MJExtension.h"

#ifndef kIconObserverPlaceHolder
#define kIconObserverPlaceHolder @"YijuDefault.jpg"
#endif

#ifndef kIconFadeTime
#define kIconFadeTime 0.5f
#endif

static const void *iconNameKey = &iconNameKey;
static const void *placeHolderNameKey = &placeHolderNameKey;

@implementation UIImageView (IconObserver)
#pragma message("这里还需要进行修改")
#pragma mark - Private
/**
 *  产生icon的完全地址
 *
 *  @param iconPath 服务器传送过来的icon相对路径
 *
 *  @return icon的绝对http路径
 */
- (NSString *)generateIconPath:(NSString *)iconPath{
    NSString *iconUrl = iconPath;
    if (![iconPath containsString:@"http:"]) {
        iconUrl = [[IconContext sharedInstance] iconByName:iconPath];
    }
    return iconUrl;
}

/**
 *  更新当前的图片
 */
- (void)updateImage{
    //kvc for parameter
    IconContext *iconContext = [IconContext sharedInstance];
    NSString *iconPath = [iconContext valueForKey:self.iconKey];
    iconPath = [self generateIconPath:iconPath];
//    NSLog(@"地址：%@++++++++++++++++++++++",iconPath);
    [self sd_setImageWithURL:[NSURL URLWithString:iconPath]
            placeholderImage:[UIImage imageNamed:self.placeHolderName]
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                       self.alpha = 0.0;
                       [UIView animateWithDuration:kIconFadeTime animations:^{
                           self.alpha = 1.0;
                       }];
                   }];

}

#pragma mark - Public
/**
 *  设置需要现实的icon
 *
 *  @param iconName        icon的名字，应该与IconContext 里面的属性相同
 *  @param placeHolderName 默认图片，给定名字
 */
- (void)setIconWithKey:(NSString *)iconKey withPlaceHolderString:(NSString *)placeHolderName{
    if (iconKey == nil) {
        return;
    }
    IconContext *iconContext = [IconContext sharedInstance];
    NSDictionary *iconDic = [iconContext mj_keyValuesWithIgnoredKeys:@[@"loadManager",@"checkManager"]];
    
    if (![[iconDic allKeys] containsObject:iconKey]) {
        DDLogWarn(@"当前iconKey:%@在IconContext未找到,请设置正确的iconKey",iconKey);
        return;
    }
    objc_setAssociatedObject(self, iconNameKey, iconKey, OBJC_ASSOCIATION_COPY);
    objc_setAssociatedObject(self, placeHolderNameKey, placeHolderName, OBJC_ASSOCIATION_COPY);
    [self updateImage];
    
}

#pragma mark - Setter
@dynamic iconKey;
- (void)setIconKey:(NSString *)iconKey{
   objc_setAssociatedObject(self, iconNameKey, iconKey, OBJC_ASSOCIATION_COPY);
}

@dynamic placeHolderName;
- (void)setPlaceHolderName:(NSString *)placeHolderName{
    objc_setAssociatedObject(self, placeHolderNameKey, placeHolderName, OBJC_ASSOCIATION_COPY);
}

#pragma mark - Getter
- (NSString *)iconKey{
    return objc_getAssociatedObject(self, iconNameKey);
}

- (NSString *)placeHolderName{
    return objc_getAssociatedObject(self, placeHolderNameKey);
}


@end
